package com.uade.magicCompany.dao;

import java.util.List;

import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

import org.apache.log4j.Logger;

import com.uade.magicCompany.entities.UsuarioEntity;

public class DAOUsuariosImpl extends
		AbstractBaseGenericDAOImpl<UsuarioEntity, Long> implements DAOUsuarios {

	private static Logger log = Logger.getLogger(DAOUsuariosImpl.class);

	@Override
	public List<UsuarioEntity> listarUsuarios() {
		// Get the entity manager for the tests.
		emf = Persistence.createEntityManagerFactory("JEE6Demo-Persistence");
		em = emf.createEntityManager();
		try {
			// Get a new transaction
			EntityTransaction trx = em.getTransaction();

			// Start the transaction
			trx.begin();
			// Get list
			Query query = em.createQuery("SELECT c FROM UsuarioEntity",
					UsuarioEntity.class);
			List<UsuarioEntity> usuarios = query.getResultList();
			// Commit and end the transaction
			trx.commit();
			return usuarios;
		} catch (RuntimeException e) {
			// if (trx != null && trx.isActive()) {
			// trx.rollback();
			// }
			throw e;
		} finally {
			// Close the manager
			em.close();
			emf.close();
		}

	}

	@Override
	public UsuarioEntity obtenerUsuarioPorNombre(String loginName) {
		// Get the entity manager for the tests.
		emf = Persistence.createEntityManagerFactory("JEE6Demo-Persistence");
		em = emf.createEntityManager();
		try {
			// Get a new transaction
			EntityTransaction trx = em.getTransaction();

			// Start the transaction
			trx.begin();
			// Get list
			Query query = em.createQuery("SELECT c FROM UsuarioEntity c WHERE c.nombre= :nombre").setParameter("nombre", loginName);
			List<UsuarioEntity> usuarios = query.getResultList();
			// Commit and end the transaction
			trx.commit();
			if(usuarios!=null && !usuarios.isEmpty()){
				return usuarios.get(0);
			}else{
				log.info("No existen usuario con el nombre: " + loginName);
				return null;
			}
		} catch (RuntimeException e) {
			// if (trx != null && trx.isActive()) {
			// trx.rollback();
			// }
			throw e;
		} finally {
			// Close the manager
			em.close();
			emf.close();
		}
	}

	@Override
	public List<UsuarioEntity> obtenerUsuarioPorGrupo(Long id) {
		// Get the entity manager for the tests.
		emf = Persistence.createEntityManagerFactory("JEE6Demo-Persistence");
		em = emf.createEntityManager();
		try {
			// Get a new transaction
			EntityTransaction trx = em.getTransaction();

			// Start the transaction
			trx.begin();
			// Get list
			Query query = em.createQuery("SELECT c FROM UsuarioEntity c WHERE c.grupoUsuario.id= :id").setParameter("id", id);
			List<UsuarioEntity> usuarios = query.getResultList();
			// Commit and end the transaction
			trx.commit();
			if(usuarios!=null && !usuarios.isEmpty()){
				return usuarios;
			}else{
				log.info("No existen usuario con el nombre: " + id);
				return null;
			}
		} catch (RuntimeException e) {
			// if (trx != null && trx.isActive()) {
			// trx.rollback();
			// }
			throw e;
		} finally {
			// Close the manager
			em.close();
			emf.close();
		}
	}

}
